package com.core.framework.web.easyweb;

import java.util.Hashtable;
import java.util.Set;
import com.core.framework.web.easyweb.annotation.Namespace;
/**
 * 
 * @author 刘涛
 * @since Jun 12, 2012
 */
public class ActionFactory implements EasywebStatics {
    	private  Hashtable<String, Object> webActionMap = new Hashtable<String,Object>();
    	private  Hashtable<String, Class<?>> webActionClassMap = new Hashtable<String,Class<?>>();
		private static ActionFactory    instance ;
		public static synchronized ActionFactory getInstance()
		{
			if(instance==null)
			{
				instance = new ActionFactory();
			}
			return instance;
		}
		public ActionFactory (){
			Set<Class<?>>  webClassSet = ClassScan.getClassScan().getWebClassSet();
			webActionMap = createActionMap(webClassSet);
		}
		public Hashtable<String,Object>  createActionMap(Set<Class<?>> webClassSet)
		{
			for(Class<?> cla  : webClassSet)
			{
			       if (cla.isAnnotationPresent(Namespace.class)) {
		                Namespace name = (Namespace) cla.getAnnotation(Namespace.class);
		                String nameValue = cla.getSimpleName();
		                nameValue = nameValue.replaceAll("Action$", "").toLowerCase();
		                nameValue = name.value() + "/" + nameValue;
		                if (webActionMap.containsKey(nameValue)) {
		                    throw new RuntimeException("Action地址定名冲突 > [现有：key=" + nameValue + " : " + cla + " 原有：" + webActionMap.get(nameValue) + "]");
		                }
		                webActionMap.put(nameValue, cla);
		                webActionClassMap.put(nameValue, cla);
		            }
			}
			return webActionMap;
		}
		public  Hashtable<String, Object> getWebActionMap() {
			return webActionMap;
		}
		
		public Hashtable<String, Class<?>> getWebActionClassMap() {
			return webActionClassMap;
		}
		public static void  main(String[] args )
		{
			System.out.println(ActionFactory.getInstance().getWebActionMap());
		}

}
